﻿
using System.Windows;

namespace Labthink.PCSoft.UniversalHost4.UserControls.MsgBox
{
    public enum MyMessageBoxButton
    {
        Ok = 0,
        OkCancel = 1,
        YesNo = 2,
        YesNoCancel = 3
    }

    /// <summary>
    /// MessageBox显示的图标类型
    /// </summary>
    public enum MyMessageBoxImage
    {
        None = 0,
        Error = 1,
        Question = 2,
        Warning = 3
    }

    /// <summary>
    /// 消息的重点显示按钮
    /// </summary>
    public enum MyMessageBoxDefaultButton
    {
        None = 0,
        Ok = 1,
        Cancel = 2,
        Yes = 3,
        No = 4
    }

    /// <summary>
    /// 消息框的返回值
    /// </summary>
    public enum MyMessageBoxResult
    {
        //用户直接关闭了消息窗口
        None = 0,
        //用户点击确定按钮
        Ok = 1,
        //用户点击取消按钮
        Cancel = 2,
        //用户点击是按钮
        Yes = 3,
        //用户点击否按钮
        No = 4
    }

    public class MyMessageBox
    {
        /// <summary>
        /// 显示消息框
        /// </summary>
        /// <param name="ower"></param>
        /// <param name="cmessageBoxText">消息内容</param>
        public static MyMessageBoxResult Show(Window ower,string cmessageBoxText)
        {
            FrmMessageBox window = null;
            Application.Current.Dispatcher.Invoke(() =>
            {
                window = new FrmMessageBox
                {
                    Owner = ower
                };
            });

            window.MyMessageBoxImage = MyMessageBoxImage.Warning;
            window.MessageBoxText = cmessageBoxText;
            window.OkButtonVisibility = Visibility.Visible;
            Application.Current.Dispatcher.Invoke(() =>
            {
                window.ShowDialog();
            });
            return window.Result;
        }

        /// <summary>
        /// 显示消息框
        /// </summary>
        /// <param name="ower"></param>
        /// <param name="cmessageBoxText">消息内容</param>
        /// <param name="caption">消息标题</param>
        public static MyMessageBoxResult Show(Window ower, string cmessageBoxText, string caption)
        {
            FrmMessageBox window = null;
            Application.Current.Dispatcher.Invoke(() =>
            {
                window = new FrmMessageBox();
            });

            window.MessageBoxText = cmessageBoxText;
            window.MessageBoxTitle = caption;
            window.MyMessageBoxImage = MyMessageBoxImage.Warning;
            window.OkButtonVisibility = Visibility.Visible;
            Application.Current.Dispatcher.Invoke(() =>
            {
                window.ShowDialog();
            });
            return window.Result;
        }

        private static void ChangeMessageBoxButton(FrmMessageBox window, MyMessageBoxButton myMessageBoxButton)
        {
            switch (myMessageBoxButton)
            {
                case MyMessageBoxButton.Ok:
                    {
                        window.OkButtonVisibility = Visibility.Visible;
                        window.OKButton.IsDefault = true;
                        break;
                    }
                case MyMessageBoxButton.OkCancel:
                    {
                        window.OkButtonVisibility = Visibility.Visible;
                        window.CancelButtonVisibility = Visibility.Visible;
                        window.OKButton.IsDefault = true;
                        window.CancelButton.IsCancel = true;
                        break;
                    }
                case MyMessageBoxButton.YesNo:
                    {
                        window.YesButtonVisibility = Visibility.Visible;
                        window.NoButtonVisibility = Visibility.Visible;
                        window.YesButton.IsDefault = true;
                        window.NoButton.IsCancel = true;
                        break;
                    }
                case MyMessageBoxButton.YesNoCancel:
                    {
                        window.YesButtonVisibility = Visibility.Visible;
                        window.NoButtonVisibility = Visibility.Visible;
                        window.CancelButtonVisibility = Visibility.Visible;
                        window.YesButton.IsDefault = true;
                        window.CancelButton.IsCancel = true;
                        break;
                    }
                default:
                    {
                        window.OkButtonVisibility = Visibility.Visible;
                        break;
                    }
            }
        }

        private static void ChangeMessageBoxDefaultButton(FrmMessageBox window,
            MyMessageBoxDefaultButton myMessageBoxDefaultButton)
        {
            switch (myMessageBoxDefaultButton)
            {
                case MyMessageBoxDefaultButton.Ok:
                    {
                        window.OkButtonStyle = ButtonStyle.NormalButtonStyle;
                        window.CancelButtonStyle = ButtonStyle.NotNormalButtonStyle;
                        window.YesButtonStyle = ButtonStyle.NotNormalButtonStyle;
                        window.NoButtonStyle = ButtonStyle.NotNormalButtonStyle;
                        break;
                    }
                case MyMessageBoxDefaultButton.Cancel:
                    {
                        window.OkButtonStyle = ButtonStyle.NotNormalButtonStyle;
                        window.CancelButtonStyle = ButtonStyle.NormalButtonStyle;
                        window.YesButtonStyle = ButtonStyle.NotNormalButtonStyle;
                        window.NoButtonStyle = ButtonStyle.NotNormalButtonStyle;
                        break;
                    }
                case MyMessageBoxDefaultButton.Yes:
                    {
                        window.OkButtonStyle = ButtonStyle.NotNormalButtonStyle;
                        window.CancelButtonStyle = ButtonStyle.NotNormalButtonStyle;
                        window.YesButtonStyle = ButtonStyle.NormalButtonStyle;
                        window.NoButtonStyle = ButtonStyle.NotNormalButtonStyle;
                        break;
                    }
                case MyMessageBoxDefaultButton.No:
                    {
                        window.OkButtonStyle = ButtonStyle.NotNormalButtonStyle;
                        window.CancelButtonStyle = ButtonStyle.NotNormalButtonStyle;
                        window.YesButtonStyle = ButtonStyle.NotNormalButtonStyle;
                        window.NoButtonStyle = ButtonStyle.NormalButtonStyle;
                        break;
                    }
                case MyMessageBoxDefaultButton.None:
                    {
                        break;
                    }
            }
        }

        /// <summary>
        /// 显示消息框
        /// </summary>
        /// <param name="ower"></param>
        /// <param name="cmessageBoxText">消息内容</param>
        /// <param name="myMessageBoxButton">消息框按钮</param>
        public static MyMessageBoxResult Show(Window ower, string cmessageBoxText, MyMessageBoxButton myMessageBoxButton)
        {
            FrmMessageBox window = null;
            Application.Current.Dispatcher.Invoke(() =>
            {
                window = new FrmMessageBox
                {
                    Owner = ower
                };
            });
            window.MyMessageBoxImage = MyMessageBoxImage.Warning;
            window.MessageBoxText = cmessageBoxText;
            ChangeMessageBoxButton(window, myMessageBoxButton);
            Application.Current.Dispatcher.Invoke(() =>
            {
                window.ShowDialog();
            });
            return window.Result;
        }

        /// <summary>
        /// 显示消息框
        /// </summary>
        /// <param name="ower"></param>
        /// <param name="cmessageBoxText">消息内容</param>
        /// <param name="myMessageBoxButton">消息框按钮</param>
        /// <param name="myMessageBoxImage">消息框图标</param>
        /// <returns></returns>
        public static MyMessageBoxResult Show(Window ower, string cmessageBoxText, MyMessageBoxButton myMessageBoxButton, MyMessageBoxImage myMessageBoxImage)
        {
            FrmMessageBox window = null;
            Application.Current.Dispatcher.Invoke(() =>
            {
                window = new FrmMessageBox
                {
                    Owner = ower
                };
            });

            window.MessageBoxText = cmessageBoxText;
            window.MyMessageBoxImage = myMessageBoxImage;
            ChangeMessageBoxButton(window, myMessageBoxButton);
            Application.Current.Dispatcher.Invoke(() =>
            {
                window.ShowDialog();
            });
            return window.Result;
        }

        /// <summary>
        /// 显示消息框
        /// </summary>
        /// <param name="ower"></param>
        /// <param name="cmessageBoxText">消息内容</param>
        /// <param name="myMessageBoxButton">消息框按钮</param>
        /// <param name="myMessageBoxImage">消息框图标</param>
        /// <param name="myMessageBoxDefaultButton">消息框默认按钮</param>
        /// <returns></returns>
        public static MyMessageBoxResult Show(Window ower, string cmessageBoxText,  MyMessageBoxButton myMessageBoxButton, MyMessageBoxImage myMessageBoxImage, MyMessageBoxDefaultButton myMessageBoxDefaultButton)
        {
            FrmMessageBox window = null;
            Application.Current.Dispatcher.Invoke(() =>
            {
                window = new FrmMessageBox
                {
                    Owner = ower
                };
            });
            window.MessageBoxText = cmessageBoxText;
            window.MyMessageBoxImage = myMessageBoxImage;
            #region 按钮
            ChangeMessageBoxButton(window, myMessageBoxButton);
            #endregion

            #region 默认按钮
            ChangeMessageBoxDefaultButton(window, myMessageBoxDefaultButton);
            #endregion

            Application.Current.Dispatcher.Invoke(() =>
            {
                window.ShowDialog();
            });
            return window.Result;
        }


        /// <summary>
        /// 显示消息框
        /// </summary>
        /// <param name="ower"></param>
        /// <param name="cmessageBoxText">消息内容</param>
        /// <param name="caption">消息标题</param>
        /// <param name="myMessageBoxButton">消息框按钮</param>
        public static MyMessageBoxResult Show(Window ower, string cmessageBoxText, string caption, MyMessageBoxButton myMessageBoxButton)
        {
            FrmMessageBox window = null;
            Application.Current.Dispatcher.Invoke(() =>
            {
                window = new FrmMessageBox
                {
                    Owner = ower
                };
            });
            window.MessageBoxText = cmessageBoxText;
            window.MessageBoxTitle = caption;
            window.MyMessageBoxImage = MyMessageBoxImage.Warning;
            ChangeMessageBoxButton(window, myMessageBoxButton);
            Application.Current.Dispatcher.Invoke(() =>
            {
                window.ShowDialog();
            });
            return window.Result;
        }

        /// <summary>
        /// 显示消息框
        /// </summary>
        /// <param name="ower"></param>
        /// <param name="cmessageBoxText">消息内容</param>
        /// <param name="caption">消息标题</param>
        /// <param name="myMessageBoxButton">消息框按钮</param>
        /// <param name="myMessageBoxImage">消息框图标</param>
        /// <returns></returns>
        public static MyMessageBoxResult Show(Window ower, string cmessageBoxText, string caption, MyMessageBoxButton myMessageBoxButton, MyMessageBoxImage myMessageBoxImage)
        {
            FrmMessageBox window = null;
            Application.Current.Dispatcher.Invoke(() =>
            {
                window = new FrmMessageBox
                {
                    Owner = ower
                };
            });

            window.MessageBoxText = cmessageBoxText;
            window.MessageBoxTitle = caption;
            window.MyMessageBoxImage = myMessageBoxImage;
            ChangeMessageBoxButton(window, myMessageBoxButton);
            Application.Current.Dispatcher.Invoke(() =>
            {
                window.ShowDialog();
            });
            return window.Result;
        }

        /// <summary>
        /// 显示消息框
        /// </summary>
        /// <param name="ower"></param>
        /// <param name="cmessageBoxText">消息内容</param>
        /// <param name="caption">消息标题</param>
        /// <param name="myMessageBoxButton">消息框按钮</param>
        /// <param name="myMessageBoxImage">消息框图标</param>
        /// <param name="myMessageBoxDefaultButton">消息框默认按钮</param>
        /// <returns></returns>
        public static MyMessageBoxResult Show(Window ower, string cmessageBoxText, string caption, MyMessageBoxButton myMessageBoxButton, MyMessageBoxImage myMessageBoxImage, MyMessageBoxDefaultButton myMessageBoxDefaultButton)
        {
            FrmMessageBox window = null;
            Application.Current.Dispatcher.Invoke(() =>
            {
                window = new FrmMessageBox
                {
                    Owner = ower
                };
            });
            window.MessageBoxText = cmessageBoxText;
            window.MessageBoxTitle = caption;
            window.MyMessageBoxImage = myMessageBoxImage;
            #region 按钮
            ChangeMessageBoxButton(window, myMessageBoxButton);
            #endregion

            #region 默认按钮
            ChangeMessageBoxDefaultButton(window, myMessageBoxDefaultButton);
            #endregion

            Application.Current.Dispatcher.Invoke(() =>
            {
                window.ShowDialog();
            });
            return window.Result;
        }
    }
}
